import {MyHttp} from '../../services/myHttp/myhttp.service';
import { Injectable}  from '@angular/core';
import { Major} from '../../services/model/major';
import { Grade} from '../../services/model/grade';
import { Course} from '../../services/model/course';
import { Score} from '../../services/model/score';
@Injectable()
export class CourseService{
  constructor(private http:MyHttp){

  }
  getMajors():Promise<Major[]>{
    return this.http.get({
      api:this.http.api.majors
    }).toPromise()
    .then((res)=>{
      let majors=[];
      if (res.status === 200) {
        let result=res.json();
        let seq=result.data;
        for(let mj of seq){
          let major=new Major();
          major.majorId=mj.majorId;
          major.major=mj.major;
          majors.push(major);
        }
      }
      return Promise.resolve(majors);
    })
  }
  getGrades():Promise<Grade[]>{
    return this.http.get({
      api:this.http.api.grades
    }).toPromise()
      .then((res)=>{
        let grades=[];
        if (res.status === 200) {
          let result=res.json();
          let seq=result.data;
          for(let gd of seq){
            let grade=new Grade();
            grade.gradeId=gd.gradeId;
            grade.name=gd.name;
            grades.push(grade);
          }
        }
        return Promise.resolve(grades);
      })
  }
  query(body:any):Promise<{count:number,items:Course[]}>{
    return this.http.post({
      api:this.http.api.queryCourse,
      body:body
    }).toPromise()
      .then((res)=> {
        if (res.status === 200) {
            let result=res.json();
          console.log(result);
            if(result.status){
              let items=result.data.items;
              let count=result.data.count;
              let courses=[];
              for(let o in items){
                let item=new Score();
                item.courseId=items[o].courseId;
                item.courseName=items[o].courseName;
                item.majorId=items[o].majorId;
                item.major=items[o].major;
                item.gradeId=items[o].gradeId;
                item.gradeName=items[o].gradeName;

                courses.push(item);
              }
              console.log(courses);
              return Promise.resolve({count:count,items:courses});
            }
        }

      });
  }




  add(body:any):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.addCourse,
      body:body
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'添加失败'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }
  update(body:any):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.updateCourse,
      body:body
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'修改失败！'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }
  remove(id:number):Promise<{status:boolean,message:string}>{
    return this.http.post({
      api:this.http.api.removeCourse,
      body:{id:id}
    }).toPromise()
      .then((res)=>{
        let data={
          status:false,
          message:'删除失败！'
        };
        if (res.status === 200) {
          let result=res.json();
          data.status=result.status;
          data.message=result.message;
        }
        return Promise.resolve(data);
      })
  }



}
